<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 
class Ceeclass{
	public $openid;
	public $openkey;
	public $pf;
	public $parameter;
	public $gp;
	public $openapi;
	//
	function  __construct() {
		require_once 'OpenApiV3.php';
        require_once 'Tencent.php';
        $CI =& get_instance();
        $CI->config->load('ceeconfig',TRUE);
		$this->openapi=new OpenApiV3($CI->config->item('appid','ceeconfig'), $CI->config->item('appkey','ceeconfig'));
		$this->openapi->setServerName($CI->config->item('server','ceeconfig'));
	}
	//获取用户基本资料
	public function getuserinfo(){
		if($this->pf == "tapp"){
			$_SESSION['t_openid'] = $this->openid;
			$_SESSION['t_openkey'] = $this->openkey;
			$CI =& get_instance();
			$CI->config->load('ceeconfig',TRUE);
			OAuth::init($CI->config->item('appid','ceeconfig'), $CI->config->item('appkey','ceeconfig'));
			//验证授权
			$r = OAuth::checkOAuthValid();
			//var_dump($r);
			if($r){
				//用户已授权，获取用户信息
				$userinfo = json_decode(Tencent::api('user/info'),true);
				$userinfo['figureurl'] = $userinfo['data']['head']."/50";
				$userinfo['nickname'] = $userinfo['data']['nick'];
				$_SESSION['name'] = $userinfo['data']['name'];
			}else{
		        	die("未授权");
			}
		}else{
			$userinfo = $this->openapi->api('/v3/user/get_info',$this->gp);
		}
		if($userinfo['ret'] != 0) exit($userinfo['ret'].' | '.$userinfo['msg'].' | /v3/user/get_info');
		return $userinfo;
	}
	//是否为某个认证空间的粉丝
	public function isQQFans($qq){
		$this->gp = array_merge($this->gp,array('page_id'=>$qq));
		$isfans = $this->openapi->api('/v3/page/is_fans',$this->gp);
		if($isfans['ret'] != 0) exit($isfans['ret'].' | '.$isfans['msg'].' | /v3/page/is_fans');
		return $isfans=$isfans['is_fans'];
	}
	//是否邀请好友成功
	public function verifyInvkey($invkey, $itime, $iopenid){
		$this->gp = array_merge($this->gp,array('invkey'=>$invkey,'itime'=>$itime,'iopenid'=>$iopenid)); //array('openid'=>$this->openid,'openkey'=>$this->openkey,'pf'=>$this->pf,'invkey'=>$invkey,'itime'=>$itime,'iopenid'=>$iopenid);
		$verifyInvkey_rs = $this->openapi->api('/v3/spread/verify_invkey',$this->gp);
		if($verifyInvkey_rs['ret']==0){
			return $verifyInvkey_rs['is_right'];
		}else{
			return -1;
		}
	}
	//文字过滤
	public function getwordfilter($str){
		$this->gp = array_merge($this->gp,array('content'=>$str));
		$getwordfilter = $this->openapi->api('/v3/csec/word_filter',$this->gp);
		if($getwordfilter['ret'] != 0) {exit($getwordfilter['msg'].' | /v3/csec/word_filter');}
		elseif($getwordfilter['is_dirty']==1){
			$str=$getwordfilter['msg'];
		}
		return $str;
	}
	//是否安装了QQ提醒，是否开启了QQ提醒
	public function isReminderSet($cmd=2){
		//$this->gp = array_merge($this->gp,array('cmd'=>$cmd));
		//$rs = $this->openapi->api('/v3/spread/is_reminder_set',$this->gp);
		//if($rs['ret'] != 0) {exit($rs['ret'].' | /v3/spread/is_reminder_set');}
		//return array("setup"=>$rs['setup'],"reg"=>$rs['reg']);
		return 1;
	}
	//设置提醒时间
	public function setReminder($Ttime=null){
		$Ttime = $Ttime?$Ttime:time();
		//$x = date("d")%3;
		$this->gp = array_merge($this->gp,array('begin0'=>$Ttime,'key0'=>$Ttime,'type0'=>20001,'title0'=>'兑换开始啦，赶紧来参与吧~~~'));
		$rs = $this->openapi->api('/v3/spread/set_reminder',$this->gp);
		//if($rs['ret'] != 0) exit($rs['msg'].' | /v3/spread/set_reminder');
		//var_dump($Ttime);
		return $rs;
	}
	//是否黄钻
	public function ishuangzuan(){
	    $rs = $this->openapi->api('/v3/user/is_vip',$this->gp);
	    if($rs['ret'] != 0) exit($rs['msg'].' | /v3/user/is_vip');
	    if($rs['is_yellow_vip']==1 || $rs['is_yellow_year_vip']==1 || $rs['is_yellow_high_vip']==1){
	        return TRUE;
	    }else{
	    	return FALSE;
	    }
	}
	//是否添加到面板
	public function isapponpanel(){
	    $rs = $this->openapi->api('/v3/spread/is_app_onpanel',$this->gp);
	    if($rs['ret'] != 0) exit($rs['msg'].' | /v3/user/is_vip');
	    return $rs['in_applist'];
	}
	//v3/user/is_login验证用户的登录态
	public function islogin(){
	    $rs = $this->openapi->api('/v3/user/is_login',$this->gp);
	    //if($rs['ret'] != 0) echo "请重新登录，relogin...";
	    return $rs;
	}
}